#!/bin/bash

# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
# kata_integration is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Description: make kuasar initrd

script_dir="$(dirname $(readlink -f $0))"
rpmlist=${script_dir}/cc-rpm.list

IMAGE_NAME=${IMAGE_NAME:-rootfs.img}
ROOTFS_DIR=${ROOTFS_DIR:-/tmp/kuasar-rootfs}

# create a temp dir to store rootfs
rm -rf ${ROOTFS_DIR}
mkdir -p ${ROOTFS_DIR}/lib \
	  ${ROOTFS_DIR}/lib64 \
	  ${ROOTFS_DIR}/lib/modules

mkdir -m 0755 -p ${ROOTFS_DIR}/dev \
	  ${ROOTFS_DIR}/sys \
	  ${ROOTFS_DIR}/sbin \
	  ${ROOTFS_DIR}/bin \
	  ${ROOTFS_DIR}/tmp \
	  ${ROOTFS_DIR}/proc \
	  ${ROOTFS_DIR}/etc


touch ${ROOTFS_DIR}/etc/resolv.conf
 
if [ ! -f "${script_dir}/vmm-task" ];then
	echo "vmm-task doesn't exist!"
	exit 1
fi


# busybox
cp /sbin/busybox ${ROOTFS_DIR}/sbin/
cp ${script_dir}/vmm-task ${ROOTFS_DIR}/sbin/init
# glibc-devel glibc
cp /lib64/libnss_dns* ${ROOTFS_DIR}/lib64
cp /lib64/libnss_files* ${ROOTFS_DIR}/lib64

# cp run request files in initrd
# 读取rpmlist文件，并赋值给rpm变量
cat $rpmlist | while read rpm
do
    # 检查rpm是否以"#"开头，不以"#"开头则走进去
    if [ "${rpm:0:1}" != "#" ]; then
        # 检查包是否存在
        rpm -ql $rpm > /dev/null 2>&1
        # 不存在就跳过继续下一个
        if [ $? -ne 0 ]; then
            continue
        fi
        # rpm -ql命令列出已安装软件包中的所有文件
        array=($(rpm -ql $rpm| grep -v "share" | grep -v ".build-id"))
        echo ${array}
        # 将包安装后所有要用到的文件拷贝到rootfs中
        for file in ${array[@]};
        do
            source=$file
            dts_file=${ROOTFS_DIR}$file
            dts_folder=${dts_file%/*}
            if [ ! -d "$dts_folder" ];then
                mkdir -p $dts_folder
            fi
            cp -r -f -d $source $dts_folder
        done
    fi
done

#create symlinks to busybox
# 在一个精简的根文件系统（root filesystem）中使用BusyBox提供的多种工具，而不需要为每种工具单独编译和安装一个独立的二进制文件。
# BusyBox是一个集成了许多常见Linux命令的单个可执行文件，通过不同的启动参数可以模拟出多个命令的行为。
BUSYBOX_BINARIES=(/bin/sh /bin/mount /bin/umount /bin/ls /bin/ps /bin/tar /bin/hwclock /sbin/modprobe /sbin/depmod /bin/ip /bin/modinfo /bin/insmod /bin/rmmod)
for bin in ${BUSYBOX_BINARIES[@]}
do
 	mkdir -p ${ROOTFS_DIR}/`dirname ${bin}`
 	ln -sf /sbin/busybox ${ROOTFS_DIR}/${bin}
done

LDD_BINARIES=(/init /sbin/busybox /usr/bin/bash /usr/bin/runc /usr/bin/attestation-agent)
for bin in ${LDD_BINARIES[@]}
 do
    # 使用ldd命令获取指定二进制所有的共享库，并将共享库解析复制到rootfs中
     ldd ${ROOTFS_DIR}${bin} | while read line
     do
 	    arr=(${line// / })

 	    for lib in ${arr[@]}
 	    do
 			echo $lib
 		    if [ "${lib:0:1}" = "/" ]; then
 			    dir=${ROOTFS_DIR}`dirname $lib`
 			    mkdir -p "${dir}"
 			    cp -f $lib $dir
 		    fi
 	    done
     done
 done

dd if=/dev/zero of=/tmp/rootfs.img bs=1M count=5000
cd /tmp
mkfs.ext4 rootfs.img
mkdir -p rootfs1
modprobe loop
losetup -D
mount rootfs.img rootfs1
cp -rfp /tmp/kuasar-rootfs/* rootfs1/
umount rootfs1
e2fsck -p -f rootfs.img
resize2fs -M rootfs.img
cp rootfs.img ${script_dir}
